import request from '@/utils/request'

export const LOGIN_TYPE = 'login'
export const LOGOUT_TYPE = 'logout'

// 同步写法
export const login = function(payload){
    console.log('LOGIN',payload);
    return {
        type:LOGIN_TYPE,
        payload
    }
}
export const logout = function(props){
    props.history.push('/login')
    return {
        type:LOGOUT_TYPE,
    }
}
export const updateUser = function(){
    return {
        type:'update'
    }
}

// 异步写法：必须使用redux-thunk中间件
// 让dispatch支持函数
export function loginAsync(params){
    console.log(params);
    return async function(dispatch){
        const {data} = await request.post('/admin/login',{
            ...params
        })

        dispatch(login(data.data))
        return data;
    }
}

export default {
    login,
    logout,
    loginAsync,
}